﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.SqlClient;
using System.Text;

public partial class Manage_ImportanceMessageShow : System.Web.UI.Page
{
    private long  accountID;
    private string areaID,accountType;
    protected void Page_Load(object sender, EventArgs e)
    {
         System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                accountID = _Account.AccountId;
                areaID = _Account.AreaId;
                accountType = _Account.Type;
                if (!IsPostBack)
                {
                    BindArea();
                }

                string curaccountid = dlHospital.SelectedItem==null?"":dlHospital.SelectedItem.Value;
                string curhospital = dlHospital.SelectedItem == null ? "" : dlHospital.SelectedItem.Text;
                BindHospital(dlArea.SelectedValue);
                if (dlHospital.Items.Contains(new ListItem(curhospital, curaccountid))) { dlHospital.Items.FindByValue(curaccountid).Selected = true; }
                showData();
            }
        }
    }

    /// <summary>
    /// 绑定地区 
    /// </summary>
    private void BindArea()
    {
        string sql ="";
        if (accountType == "1" || accountType == "0")
        {
            sql = "select areaid,areaname from area  where areaid >530000 and areaid<540000 and areaid%100=0";
        }
        else if(accountType=="4")
        {
            sql = "select areaid,areaname from area  where areaid ="+areaID;
        }
        Db db = new Db();
        DataTable dt = db.DataTable(sql);
        db.Close();

        if (accountType == "1")
        {
            dt.Rows.Add(new string[]{"0", "--全部--"});
        }
        dlArea.DataSource = dt;
        dlArea.DataTextField = "areaname";
        dlArea.DataValueField = "areaid";
        dlArea.DataBind();
        dlArea.Items[dt.Rows.Count - 1].Selected = true;
    }


    private void BindHospital(string areaid)
    {
        string sql;
        if (areaid == "0") //全部地区
        {
            sql = "select accountid,hospitalname from hospital,area where hospital.areaid=area.areaid and area.areaid >530000 and area.areaid<540000 and area.areaid%100=0 order by hospitalname";
        }
        else
        {
            sql = "select accountid,hospitalname from hospital where areaid=" + areaid + " order by hospitalname";
        }
        Db db = new Db();
        DataTable dt= db.DataTable(sql);
        db.Close();

        dt.Rows.Add(new string[] { "0", "--全部--" });

        dlHospital.DataSource = dt;
        dlHospital.DataTextField = "hospitalname";
        dlHospital.DataValueField="accountid";
        dlHospital.DataBind();
        //dlHospital.Items[dt.Rows.Count - 1].Selected = true;
    }


    /// <summary>
    /// 显示数据
    /// </summary>
    private void showData()
    {
        StringBuilder sbHtml = new StringBuilder();
        sbHtml.Append("<table width='100%' border='0' align='left' cellpadding='2' cellspacing='1' class='table_table2' id='Mytable'>");
        sbHtml.Append("<tr>");
        sbHtml.Append("<td  align='center'  width='3%'>采购单名称</td>");
        sbHtml.Append("<td  align='center' width='3%'>药品流水号</td>");
        sbHtml.Append("<td  align='center' width='8%'>通用名</td>");
        sbHtml.Append("<td  align='center'  width='5%'>商品名</td>");
        sbHtml.Append("<td  align='center'  width='5%'>剂型</td>");
        sbHtml.Append("<td  align='center'  width='3%'>规格</td>");
        sbHtml.Append("<td  align='center'  width='3%'>采购医院</td>");
        sbHtml.Append("<td  align='center' width='9%'>配送企业</td>");
        sbHtml.Append("<td  align='center' width='5%'>配送企业<br/>注册号</td>");
        sbHtml.Append("<td  align='center'  width='3%'>入库数量</td>");
        sbHtml.Append("<td  align='center' width='6%'>交易价</td>");
        sbHtml.Append("<td  align='center' width='6%'>总金额</td>");
        sbHtml.Append("<td  align='center' width='6%'>入库时间</td>");
        sbHtml.Append("</tr>");



        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "BuyGoodsView_200708";      //要查询的表明
        dbPage.Key = "buyplangoodsid";     //表的主键    
        dbPage.Where = GetFilter(0);     //查询条件    
        //dbPage.SelectField = "SupplyGoodsId,ProductCode,goodsid,productname,goodsname,medicinemodel,outlookc,factor," +
                                //"GoodsName,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1," +
                                //"CASE QualityLevels WHEN '1' THEN '专利药品' WHEN '2' THEN '原研药品' WHEN '3' THEN '单独定价药品' WHEN '4' THEN '优质优价' " +
                                //"WHEN '5' THEN 'GMP药品' END AS QualityLevels ,bidprice,companyname_ps,UserCode_PS," +
                                //"companyname_sc,UserCode_SC";//要显示的字段
        dbPage.SelectField = " planname,goodsid,productname,goodsname,medicinemodel,outlookc,companyname_ps,hospitalname,inflownum,tradeprice,usercode_ps," +
                                "convert(varchar(10),companyinflowdate,120) as  inflowdate,DATEDIFF ( day , companyinflowdate , getdate() ) as outsideDate";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "planname,productname,medicinemodel,outlookc";//排序字段

        IDataReader dr = dbPage.ExecuteReader();
        int i = 0;
        while (dr.Read())
        {
            sbHtml.Append("<tr  onmouseover=\"this.style.backgroundColor='#CCFFFF';this.title='超期：" + dr["outsideDate"].ToString() + "天'\" onmouseout=\"this.style.backgroundColor='white'\" style='cursor:hand;background-color:white;'class='table_td2'>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["planname"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["goodsid"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["productname"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["goodsname"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["medicinemodel"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["outlookc"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["hospitalname"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["companyname_ps"].ToString() + "</td>");
            sbHtml.Append("<td  align='right' width='8%'>&nbsp;" + dr["usercode_ps"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["inflownum"].ToString() + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + Math.Round(double.Parse(dr["tradeprice"].ToString()),2) + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + Math.Round(double.Parse(dr["tradeprice"].ToString()) * int.Parse(dr["inflownum"].ToString()), 2) + "</td>");
            sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["inflowdate"].ToString() + "</td>");
            sbHtml.Append("</tr>");
            i++;
        }
        if (i == 0)
        {
            sbHtml.Append("<tr><td colspan='13' style='text-align:center;color:red;'>抱歉,没有查询到符合条件的信息!</td></tr></table>");
        }
        else
        {
            sbHtml.Append("</table>");
        }
        dr.Close();
        db.Close();

        dr = null;
        db = null;

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
        Label1.Text = sbHtml.ToString();
        ShowImportance();//查询多少条采购单
    }


    /// <summary>
    /// 获取查询条件 0 为 正常 1 为统计数量
    /// </summary>
    /// <returns></returns>
    private string GetFilter(int i)
    {
        string accountid_yy = dlHospital.SelectedItem.Value;
        string productname = txtProudctName.Text.Trim();
        string outlookc = txtOutLookc.Text.Trim();
        string goodsid = txtGoodsId.Text.Trim();
        string planname = txtPlanName.Text.Trim();
        string companyname_ps = txtCompanyName_ps.Text.Trim();
        string usercode_ps = txtUserCode_ps.Text.Trim();
        string goodsname = txtGoodsName.Text.Trim();
        string areaid = dlArea.SelectedItem.Value;


        StringBuilder sbFilter = new StringBuilder("DealStatus=2 and paymentid is null and DATEDIFF ( day , companyinflowdate , getdate() ) >60");
        
        if (accountid_yy != "0")
        {
            sbFilter.Append(" and accountid_yy=" + accountid_yy);
        }
        if (i == 0)
        {
            if (areaid != "0")
            {
                sbFilter.Append(" and areaid like '%" + areaid + "%'");
            }

            if (goodsid != "")
            {
                sbFilter.Append(" and goodsid='" + goodsid + "'");
            }
            if (productname != "")
            {
                sbFilter.Append(" and productname like '%" + productname + "%'");
            }

            if (outlookc != "")
            {
                sbFilter.Append(" and outlookc like '%" + outlookc + "%'");
            }


            if (planname != "")
            {
                sbFilter.Append(" and planname like '%" + planname + "%'");
            }

            if (companyname_ps != "")
            {
                sbFilter.Append(" and companyname_ps like '%" + companyname_ps + "%'");
            }

            if (usercode_ps != "")
            {
                sbFilter.Append(" and usercode_ps like '%" + usercode_ps + "%'");
            }

            if (goodsname != "")
            {
                sbFilter.Append(" and goodsname like '%" + goodsname + "%'");
            }
        }
        return sbFilter.ToString();

    }

    private void ShowImportance()
    {

        string _strse = "select distinct BuyPlanId from  BuyPlanGoods where " + GetFilter(1);
        Db db = new Db();
        DataTable dt = db.DataTable(_strse);
        db.Close();
        if (dt.Rows.Count > 0)
        {
            Label2.Text = dt.Rows.Count.ToString();
        }
    }
 
}
